package com.jyb.interceptor;

import org.apache.log4j.Logger;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jyb.config.ConstConfig;
import com.jyb.model.User;
import com.jyb.util.CheckUserUtil;
import com.jyb.vo.UserVo;

public class UserInterceptor implements Interceptor{

	private static Logger logger=Logger.getLogger(UserInterceptor.class);
	
	public void intercept(ActionInvocation ai) {
		
		UserVo userVo=(UserVo)ai.getController().getSession().getAttribute("user");
		if(userVo==null){
			String str=ai.getController().getCookie(ConstConfig.ACTIVE_USER);
			if(CheckUserUtil.isValid(str)){
				str=CheckUserUtil.decode(str);
				String[] strs=str.split(",");
				User user=User.dao.getUserByCellphone(strs[0]);
				ai.getController().getSession().setAttribute("user", User.dao.getUserVo(user));
				logger.info(strs[0]+"自动登录成功！");
			}
		}
		
		ai.invoke();
		
	}

}
